package top.infopub.mgr.mkt.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.service.DiscloseProService;
import top.infopub.mgr.mkt.service.ProAcceptService;
import top.infopub.mgr.mkt.vo.ProAcceptVo;
import top.infopub.mgr.sg.service.CoopInfoService;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.mkt.DisclosePro;
import top.infopub.model.mkt.ProAccept;
import top.infopub.model.sg.CoopInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.github.pagehelper.Page;


/**
 * 业务承接单管理
 * @author wangw
 * @version 2020年1月10日
 * @see ProAcceptController
 * @since
 */
@Controller
@RequestMapping("/proAccept")
public class ProAcceptController extends GenericFlowController {

    @Autowired
    private ProAcceptService proAcceptService;
    
    @Autowired
    private DiscloseProService disclseProService;
    
    @Autowired
    private StaffInfoService staffInfoService;
    
    @Autowired
    private CoopInfoService coopInfoService;
    
    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    /**
     * 业务承接单列表
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "mkt/lay-proAccept-list";
    }
    
    /**
     * 业务承接单列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<ProAcceptVo> queryDataByCondition(ProAcceptVo proAcceptVo, ParamPage paramPage) {
        Page<ProAcceptVo> page = new Page<ProAcceptVo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (proAcceptVo.getEndDate() != null) {
            proAcceptVo.setEndDate(DateUtils.addDays(proAcceptVo.getEndDate(), 1));
        }
        page = proAcceptService.selectPageByDomainWhere(page, proAcceptVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
    
    /**
     * 业务承接单-我审批的列表数据
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<ProAcceptVo> queryMyCheckDataByCondition(ProAcceptVo proAcceptVo, ParamPage paramPage) {
        Page<ProAcceptVo> page = new Page<ProAcceptVo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (proAcceptVo.getEndDate() != null) {
            proAcceptVo.setEndDate(DateUtils.addDays(proAcceptVo.getEndDate(), 1));
        }
        proAcceptVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = proAcceptService.selectMyCheckByDomainWhere(page, proAcceptVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
    
    /**
     * 业务承接单-新增
     * @return
     */
    @RequestMapping("/add")
    public String add(HttpServletRequest request) {
        //查询可业务承接单的记录
        List<DisclosePro> disclosePro = disclseProService.selectByEntityWhere(null);
        request.setAttribute("disclosePros", disclosePro);
        //查询可用的联合体单位记录
        CoopInfo coopInfo = new CoopInfo();
        coopInfo.setState(1);
        coopInfo.setTypes("1");
        List<CoopInfo> coopInfos = coopInfoService.selectByEntityWhere(coopInfo);
        request.setAttribute("coopInfos", coopInfos);
        return "mkt/lay-proAccept-add";
    }
    
    /**
     * 根据id查询业务承接单详情  > 注意：方法命名必须为：toDealOrDetail
     * @param id 业务承接单Id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request,String id, String processDealWith){
        Map<String, Object> map = new HashMap<String, Object>();
        ProAcceptVo proAccept = proAcceptService.selectById(id);
        map.put("proAccept", proAccept);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, proAccept.getProcessId());
        return new ModelAndView("mkt/lay-proAccept-detail", map);
    }
    
    /**
     * 新增业务承接单
     * @return
     */
    @SuppressWarnings({"rawtypes"})
    @RequestMapping("/save")
    public @ResponseBody RspVo save(HttpServletRequest request,ProAcceptVo proAccept) {
        //判断项目是否已存在
        List<ProAccept> proAccepts = proAcceptService.selectByProCode(proAccept.getProCode());
        if(CollectionUtils.isNotEmpty(proAccepts)){
            return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("项目【" + proAccept.getProName()
                + "】已添加业务承接单~");
        }
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        
        /**
         * 业务承接单主表处理
         */
        String id = IdFactory.getUUIDUpper();
        proAccept.setId(id);
        proAccept.setHrStaffApplyId(vo.getHrStaffId());
        proAccept.setHrOrgId(proAccept.getProAcceptOrgId());
        proAccept.setHrDeptId(proAccept.getProAcceptDeptId());
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(vo.getHrStaffId());
        proAccept.setStaffName(staffInfo.getStaffName());
        proAccept.setAddBy(vo.getId());
        proAccept.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String codes = codeGeneratorConfigService.generatorCode("mkt_pro_accept_codes");
        proAccept.setCodes(codes);
        // 处理processId
        proAccept.setProcessId(IdFactory.getUUIDUpper());
        proAccept.setProcessStatus("1");//审批中
        proAccept.setUptBy(vo.getId());
        proAccept.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        
        proAcceptService.addProAccept(proAccept);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "业务承接单 -新增业务承接单：项目名="+proAccept.getProName(), null);
        return RspVo.getSuccessResponse();
    }
    
    /**
     * 根据id查询业务承接单记录
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getProAcceptById")
    @ResponseBody
    public Map<String, Object> getProAcceptById(String id) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            ProAcceptVo proAcceptVo = proAcceptService.selectById(id);
            AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
            StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
            proAcceptVo.setStaffName(staffInfo.getStaffName());
            map.put("proAcceptVo", proAcceptVo);
        }
        catch (Exception e) {
            log.error("查询业务承接单记录异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }
    
}
